package com.example.smartcitymovie.common;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.RecycleViewAdapter;
import com.example.smartcitymovie.bean.MovieListBean;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @Author: Machu
 * @Time: 2022/2/14 13:19
 * @Description: 功能描述
 */
public class RecycleViewPlus<T> extends LinearLayout {
    private RecyclerView recyclerView;
    private View view;
    private RecycleViewAdapter recycleViewAdapter;
    private Context context;
    private T bean;

    public RecycleViewPlus(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.recycleview_plus_layout, this);
        recyclerView = view.findViewById(R.id.BaseRecycleView);
        this.context = context;

    }

    public void setLinearLayoutManager(int orientation) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(orientation);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    public void setGridLayoutManager(int num) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, num);
        recyclerView.setLayoutManager(gridLayoutManager);
    }

    public void setStaggeredGridLayoutManager(int span, int orientation) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(span, orientation);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
    }

    public void setAdapter(List<T> list, int ItemResource) {
        recycleViewAdapter = new RecycleViewAdapter(context, list, ItemResource);
        recyclerView.setAdapter(recycleViewAdapter);
    }


    public void setAdapter(Activity activity, String url, Class<T> tClass, int resource) {
        OkHttpUtils.getCall(url).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                bean = MyApplication.getGson().fromJson(response.body().string(), tClass);
                Class<?> beanClass = bean.getClass();
                try {
                    Method getMsg = beanClass.getMethod("getCode", new Class[]{});
                    Integer code = (Integer) getMsg.invoke(bean);
                    if (code == 200) {
                        Method[] methods = beanClass.getMethods();
                        Log.d("TAG", "onResponse: "+methods.length);
                        for (Method method : methods
                        ) {
                            if (method.getName().equals("getRows")) {
                                List<T> o = (List<T>) method.invoke(bean);
                                activity.runOnUiThread(()->{
                                    recycleViewAdapter = new RecycleViewAdapter(context, o, resource);
                                    recyclerView.setAdapter(recycleViewAdapter);
                                });
                            }

                        }
                    }
                } catch (Exception exception) {
                    Log.d("TAG", "error: " + exception);
                }

            }
        });
    }


    public RecycleViewAdapter getBaseAdapter(){
        return  recycleViewAdapter;
    }

    public T getBean() {
        return bean;
    }

    public void notifyDataSetChanged(){
        recycleViewAdapter.notifyDataSetChanged();
    }
}
